package com.hhf.riceshopping.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

import com.hhf.riceshopping.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016102100733550";

    // 商户私钥，您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCJygl2ojVg1aY4KT+xaKo+S10gQc16Nmziq8kT5noWDZlqeOjV6ePdiFYGXvRpqUhdoIO1DUnG/6ll4n2+k/1E+s9e0nlUSF1QbsaEbpwCyuuVwLAnsueqWRkieCnhaJHMpUaFnV6eQ1IkN4bgESVTBJxWUihVMazkpXqulOlUfpVeQoix4BCPlXHD/p9oR7/OP1uG3n25+JVo2y1W6P6LsKSTpwPzLXM/5JNs5kpYc5yD1UcABwEP7WH56j+sejK4G/Af6P09CfKPtHbcAuk/X1W+/st8rsUOM+IUoFrHNSoleXIgGXwozzquL0ITGYnUlUgL0NAP5wP0AYtfV92vAgMBAAECggEALP2aP3FRio/YWWgmkvAA/pwvCTRTOn+A6DZ9TpEn9gF0wY4/QHHBZHl61VO17LZImcGqn7h6CY87KbhdpJe/0AExKt4AneOPDI9PJLjVGhhV+izTl/LTsAytp1KjIiW5zZ99um0bEwQaUxcKZClVDZlozsq+vqqcsH/T4iNfWKYNPx3cQN7NQSdJLF7w3+oqOEFAXM1uNzfiDLeE4ej99uArTJh6dk/mXjMa58RUiLm+4z4VFfVQGAZnOF1oXteMSWRjSCPc4sBtj79ZUD+oDlE4k64X/msz8mykLKnHnxJ2nG6MRV4TloSOttX3pz69oYmrKUc81iN5ZUy5lwrEoQKBgQDOZUMyObY1ifJm5hzCaj990Gji2D9U8rcca1aVQN8bnCvzt33n6EjmSHLoc0j/VOGsw3UseXm3RvheAMtHXFLGjRgFXJSypcym5HbwxCYESOB+OrERIoyVIO5tqZOkyK8CyP5a/SoDxkP3M1ooa5YwtzEqom9uHeQaMiDXlwER0QKBgQCq5693mUDDd5FVQgM/4bP2sLeuynl2jh5SVIHiG+0OhOCBkoenYVKILhwtgb2XCgz+j+CGPdW/oVRAXbMpNzAVE6rFd6wi/D2jhj1Ch67bP8RA47/xvEbvB/RA5q17Cp4n2l4PLpVaKKoXBRtLVbpkZIthsrW7y9hajIIxyyJXfwKBgQCqqRSM/Sdq0au75d4164CgWanAmKqVZTR/rUmklEVnIRU+qqTTE0DkGVfaWSks51TLc7r5kWBRab+IiwOK+A4B2zzXghv6NBoqjZLDtozw0gm+lm0/C+BOlzJPPv60LLR5ixS2y8rn4BLm3nFYJs3DblabOEEHPnQULQr2+QeLYQKBgQCTZg0c3m1P6HDNEokQ8I2xJsrVEsXdbUdThJUcuS8kRZAH0KP0xWhUEUi2Kt6kRMPeTaK8xNn0U6UBeo7J7lIm63Zupv8skVZsEZwGJFW3vPB7crIaQGMBR1/vFcTriMXM43YaK/gd8VJ5VisrNz3x1hGsamB5Hp9zwRmM7oCo6wKBgQDB4H8eJngUAP61bbNG2D//8ajMmyr375HdL9UDORme1rx2axk05YYbm97W3z7hj8oPPlV00IA7sRY9/5vxexWpLTgEXpp+ZtZ3j2bdQWeOUQsgiipV2j3rwuZGh1HKd813eEESNR9nXLvFcW3ueq7eg4J6Mxe7MQLqKIAX9m1WyA==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsgDKIMiloc+lPfCxaRp/yApjmjSf5sR9Mv7m2MUBDaS6dUu9maUFriCRfG3efU141y76JP5xGqveLnB9o6XhL0Eikl9FLHRIFcHWvk8TI10wsAio7UCOnF8HsB9xLdcON4y7RwY/WWuPG8ahrldGQAjpnTulk3aY/7mAEx+gf/aKirUEOKPHyo4J/Z1PQacvUD1XsOCjJjeupSvcHFyuVn4SJKiW6CR/RnOKRA58OmLcsvtuWyI+8bX0e3QZG9Wnt0BMraOEjI5ATPa4E+8q/ok+O3Xcm5lGpmLGJ6rlc0dPuPqgNtzmUm/IGQGZW1M0zNkM8Df301skXJh9VozbdQIDAQAB";


    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://hhfpay.natapp1.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

//    private String timeout = "30m"; //线上以后，应该改为30分钟来收单
    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
